<?php

?>

<div class="well well-small span7">
<form class=" form-inline" action="<?php echo REQUEST_SELF; ?>?list_ds" method="POST" name="dsListInterval" id="dsListInterval">
    <fieldset>
        <legend>История подключений</legend>

        <!--
        <input type="hidden" name="token" id="token" value="<?php echo Arr::getValue($params, 'token', ''); ?>">
        <input type="hidden" name="pass" id="pass" value="">
        <input type="hidden" name="action" id="action" value="<?php echo ACTIONS::INSTALL_GET_DSLIST; ?>">
        -->

        <div class="control-group">
            <p>Интервал времени:</p>
            <label class="control-label" for="from_date">с <input type="text" class="input-medium" id="from_date" name="from_date" value="<?php echo Arr::getValue($params, 'from_date', ''); ?>"> </label>
            <label class="control-label" for="to_date"> по <input type="text" class="input-medium" id="to_date" name="to_date" value="<?php echo Arr::getValue($params, 'to_date', ''); ?>"> </label>

            <button type="submit" class="btn btn-primary"><i class="icon-ok-sign icon-white"></i> Показать</button>
        </div>
        <div class="control-group">
            <button type="button" class="btn btn-info" id="btn-all"><i class="icon-th-list icon-white"></i> Полный список</button>
            <button type="button" class="btn btn-info" id="btn-last30"><i class="icon-calendar icon-white"></i> За последние 30 дней</button>
            <button type="button" class="btn btn-info" id="btn-last365"><i class="icon-calendar icon-white"></i> За последний год</button>
        </div>
    </fieldset>
</form>
</div>

<script type="text/JavaScript" src="js/jquery.ui.datepicker-ru.min.js"></script>
<script type="text/javascript">
$(function() {
    Elm.todate = $('#to_date');
    Elm.fromdate = $('#from_date');
    Elm.FormDsList = $('#dsListInterval');

    $.datepicker.setDefaults($.datepicker.regional['ru']);
    Elm.fromdate.datepicker({
        dateFormat  : "yy-mm-dd",
        currentText : "Сегодня",
        closeText   : "Закрыть",
        showButtonPanel: false,
        changeYear  : true,
        changeMonth : true
    });

    Elm.todate.datepicker({
        dateFormat  : "yy-mm-dd",
        currentText : "Сегодня",
        closeText   : "Закрыть",
        showButtonPanel: false,
        changeYear  : true,
        changeMonth : true
    });

    $('#btn-all').click(function(){
        Elm.todate.datepicker( "setDate" , '0' );
        Elm.fromdate.datepicker( "setDate" , '2010-01-01' );
        Elm.FormDsList.get(0).submit();
    });

    $('#btn-last30').click(function(){
        Elm.todate.datepicker( "setDate" , '0' );
        Elm.fromdate.datepicker( "setDate" , '-30' );
        Elm.FormDsList.get(0).submit();
    });

    $('#btn-last365').click(function(){
        Elm.todate.datepicker( "setDate" , '0' );
        Elm.fromdate.datepicker( "setDate" , '-365' );
        Elm.FormDsList.get(0).submit();
    });

});



function toggle_ds(btn) {
    var sbtn = $(btn);
    var dsc = $('#'+btn.id+'-content');
    if (dsc.hasClass('hidden')) {
        dsc.removeClass('hidden');
        sbtn.find('i').attr('class','').addClass('icon-white icon-chevron-up');
        sbtn.find('span').html(' Свернуть');
    }
    else {
        dsc.addClass('hidden');
        sbtn.find('i').attr('class','').addClass('icon-white  icon-chevron-down');
        sbtn.find('span').html(' Развернуть');
    }
}
</script>








<?php
$err = Arr::getValue($params, 'error', '');
    if ($err) $err = <<<__ERR__
        <span class="help-block alert alert-error span7">$err</span>
__ERR__;
echo $err;

$rows = Arr::getValue($params, 'rows', array());

$obj_types  =  Arr::getValue($params, 'obj_types', array());
$db_types   =  Arr::getValue($params, 'db_types', array());
$apps       =  Arr::getValue($params, 'apps', array());
$oss        =  Arr::getValue($params, 'oss', array());
$btypes     =  Arr::getValue($params, 'btypes', array());
$views      =  Arr::getValue($params, 'views', array());

foreach ($rows as $row) {
    $obj_type   = Arr::getValue( $obj_types, Arr::getValue($row, 'obj_type', '0'),   array() );
    $btype      = Arr::getValue( $btypes,    Arr::getValue($row, 'build_type', '0'), array() );
    $db_type    = Arr::getValue( $db_types,  Arr::getValue($row, 'db_type', '0'),    array() );
    $app        = Arr::getValue( $apps,      Arr::getValue($row, 'app', '0'),   array() );
    $os         = Arr::getValue( $oss,       Arr::getValue($row, 'os', '0'),    array() );
    $view       = Arr::getValue( $views,     Arr::getValue($row, 'view_id', '0'),    false );


    ?>
<div class="well well-small span7" style="color:#666">
    <div>
        <h3 class="pull-right"><?php echo Arr::getValue($row, 'created', '-'); ?></h3>
        <h2><?php echo Arr::getValue($row, 'name', '-'); ?></h2>

        <button class="btn btn-info pull-right" id="exp-ds-<?php echo Arr::getValue($row, 'id', '0'); ?>" onclick="toggle_ds(this);return false;" title="Свернуть/развернуть">
            <i class=" icon-chevron-down icon-white"></i><span> Развернуть</span>
        </button>

        <strong>Адрес: </strong>
        <?php echo Arr::getValue($row, 'address', '-'); ?>
    </div>

    <div id="exp-ds-<?php echo Arr::getValue($row, 'id', '0'); ?>-content" class="hidden">
        <div class="span6" style="margin-bottom: 20px; margin-top: 20px;">
            <strong>Описание объекта</strong>
            <div><?php echo Arr::getValue($row, 'description', '-'); ?></div>
        </div>


        <div class="span3">
            <div class="vmargins">
                <strong>Таблица</strong>
                <div><?php echo Arr::getValue($row, 'tbl', '?'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Датчики</strong>
                <div><?php echo Arr::getValue($row, 'sen', '?'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Тип помещения</strong>
                <div><?php echo Arr::getValue($btype, 'name', '?'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Тип объекта</strong>
                <div><?php echo Arr::getValue($obj_type, 'name', '?'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Тип БД</strong>
                <div><?php echo Arr::getValue($db_type, 'name', '?'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Софт</strong>
                <div><?php echo Arr::getValue($app, 'name', '?'); ?></div>
            </div>

            <div class="vmargins">
                <strong>ОС</strong>
                <div><?php echo Arr::getValue($os, 'name', '?'); ?></div>
            </div>




        </div>

        <div class="span3">
            <div class="vmargins">
                <strong>Фотографии объекта</strong>
                <div><?php echo Arr::getValue($row, 'photo', '-'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Размещение компьютера</strong>
                <div><?php echo Arr::getValue($row, 'placed', '-'); ?></div>
            </div>

            <div class="vmargins">
                <strong>БД - пояснение</strong>
                <div><?php echo Arr::getValue($row, 'db_descr', '-'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Путь к БД</strong>
                <div><?php echo Arr::getValue($row, 'db_path', '-'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Операционная система - пояснение</strong>
                <div><?php echo Arr::getValue($row, 'os_descr', '-'); ?></div>
            </div>

            <div class="vmargins">
                <strong>Параметры удаленного доступа</strong>
                <div><?php echo Arr::getValue($row, 'remote', '-'); ?></div>
            </div>

        </div>







        <div id="linkedViewFor-<?php echo Arr::getValue($row, 'id', '0'); ?>" class="<?php echo $view? '' : 'hidden';  ?> span6">


                <div class="vmargins">
                    <strong>Связанное отображение:</strong>
                    <div class="linkedViewName" id="">
                        <button type="button" class="btn btn-mini btn-info button-edit-view" data-historyobj="<?php echo Arr::getValue($row, 'id', '0'); ?>" data-viewid="<?php echo Arr::getValue($view, 'id', '0'); ?>">
                            <i class="icon-edit icon-white"></i>
                        </button>
                        <span class="view-name"><?php echo Arr::getValue($view, 'obj_name', '???'); ?></span>
                    </div>
                </div>

        </div>

        <div class="clear_both"></div>


        <button data-historyobj="<?php echo Arr::getValue($row, 'id', '0'); ?>"
                class="btn btn-info pull-left <?php echo $view? 'hidden' : '';  ?> button-add-view"
                id="button-add-view-for-<?php echo Arr::getValue($row, 'id', '0'); ?>">
            <i class="icon-plus icon-white"></i>
            Создать отображение
        </button>



        <a class="pull-right btn btn-inverse" href="install.php?<?php echo ACTIONS::INSTALL_EDITDS; ?>&id=<?php echo Arr::getValue($row, 'id', '0'); ?>">
            <i class="icon-edit icon-white"></i>
            Редактировать
        </a>
    </div>





</div>

   <?php
}


?>

<?php echo includeFile(APP_PAGE_PARTS.'view_dialog_scripts.php'); ?>
<script>

    function createVwFromHis(hobj) {
        var send = {
            action: '<?php echo ACTIONS::VIEW_FROM_HIS; ?>',
            ho_id: hobj
        };
        jsonRequest(
            'views.php?VwFromHis',
            send,

            function(data) {
                if(checkSession(data)) {
                    if (!data.state) {
                        uiAlert(data.message,'Error');
                        return;
                    }

                    if (data.hasOwnProperty('response_data') && data.response_data) {
                        var resp = data.response_data;
                        if (
                            resp.hasOwnProperty('vwId') &&
                            resp.hasOwnProperty('vwName') &&
                            resp.vwId && resp.vwName
                        ) {
                            updateViewName(hobj, resp.vwId, resp.vwName);
                            showLinkedView(hobj);

                            $('#linkedViewFor-'+hobj+' button.button-edit-view').click();
                        }


                    }
                }
                else {
                    Elm.loginFormModal.dialog('open');
                    Elm.loginFormModal.successCallback = function(){
                        createVwFromHis(hobj);
                    };
                }
            },

            function() {
                uiAlert('Ошибка соединения','Error');
            }
        );
    }

    function updateViewName(hobj, id, name) {
        var div = $('#linkedViewFor-'+hobj);
        div.find('button.button-edit-view').attr('data-viewid',id);
        div.find('span.view-name').html(name);
    }

    function showLinkedView(hobj) {
        $('#button-add-view-for-'+hobj).hide();
        $('#linkedViewFor-'+hobj).removeClass('hidden');
    }



    $(function() {

        $('button.button-edit-view').click(function() {
            var $this = $(this);
            var vw_id = $this.attr('data-viewid');
            var hobj = $this.attr('data-historyobj');

            if (!vw_id || !hobj) return;


            openVwDialog(vw_id, function(cdata) {
                if (cdata.hasOwnProperty('obj_name')) {
                    updateViewName(hobj, vw_id, cdata.obj_name);
                    showLinkedView(hobj);
                    //closeVwDialog();
                }
            });

            //alert(vw_id+' '+hobj);
        });


        $('button.button-add-view').click(function() {
            var $this = $(this);
            var hobj = $this.attr('data-historyobj');
            createVwFromHis(hobj);

        });

    });
</script>